//
//  HomeSubmitViewController.m
//  AmountBody
//
//  Created by smm on 2017/12/18.
//  Copyright © 2017年 RCMTM. All rights reserved.
//

#import "HomeSubmitViewController.h"
#import "ClassModel.h"
#import "ClassForCollectionViewCell.h"
#import "SearchCollectionReusableView.h"
#define itemWidth (SCREEN_W-60)/5.0
#define itemHeight itemWidth/(100/120.0)+75
@interface HomeSubmitViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *downView;
@property (weak, nonatomic) IBOutlet UIButton *strollButton;
- (IBAction)strollButton:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UIButton *addShopButton;
- (IBAction)addShopButton:(UIButton *)sender;

@property (strong, nonatomic) UICollectionView *collectionView;
@property(nonatomic,strong)UICollectionViewLayout* layout;
@property (strong, nonatomic) NSMutableArray * dataArray;
@property (assign, nonatomic) BOOL isShow;

@end

@implementation HomeSubmitViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.public_headView.hidden = NO;
    self.public_headView.leftButton = [self leftButton];
    self.public_headView.titleLabel = [self titleLabel];
    self.view.backgroundColor = [UIColor whiteColor];
    NSArray * classArr = [NSArray arrayWithObjects:@"衣服",@"男士衬衫",@"男士西装",@"西裤",@"扣子",@"布料",@"风衣",@"毛呢", nil];
    for (int i=0; i<classArr.count; i++) {
        ClassModel * model = [[ClassModel alloc]init];
        model.name = [classArr objectAtIndex:i];
        model.class_id = [NSString stringWithFormat:@"%d",i];
        [self.dataArray addObject:model];
    }
    [self loadUI];
    // Do any additional setup after loading the view.
}
#pragma mark - 懒加载

- (NSMutableArray *)dataArray;
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray array]init];
        
    }
    return _dataArray;
}
#pragma mark -  竖屏适配
-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    CGFloat downViewH = USERDEFINED_TABBAR_H;
    self.collectionView.frame = CGRectMake(0, 64, SCREEN_W, SCREEN_H-TOPNAVIGATIONBAR_H-downViewH);
    _downView.frame = CGRectMake(-1, SCREEN_H-downViewH+1, SCREEN_W+2, downViewH);
    [self.collectionView reloadData];
    _addShopButton.frame = CGRectMake((SCREEN_W+2)-_addShopButton.frame.size.width-20, 0, _addShopButton.frame.size.width+20, downViewH);
    _strollButton.frame = CGRectMake((SCREEN_W+2)-_addShopButton.frame.size.width-(_strollButton.frame.size.width+20), 0, _strollButton.frame.size.width+20, downViewH);
}
#pragma mark -  UI布局
-(void)loadUI
{
   CGFloat downViewH = USERDEFINED_TABBAR_H;
    UICollectionViewFlowLayout  *layout = [[UICollectionViewFlowLayout alloc] init];
    // 设置具体属性
    // 1.设置 最小行间距
    layout.minimumLineSpacing = 10;
    // 2.设置 最小列间距
    layout. minimumInteritemSpacing  = 10;
    // 3.设置item块的大小 (可以用于自适应)
    layout.estimatedItemSize = CGSizeMake(itemWidth, itemHeight);
    // 设置滑动的方向 (默认是竖着滑动的)
    layout.scrollDirection =  UICollectionViewScrollDirectionVertical;
    // 设置item的内边距
    layout.sectionInset = UIEdgeInsetsMake(1,10,1,10);
    //设置collectionView头视图的大小
    [layout setHeaderReferenceSize:CGSizeMake(_collectionView.frame.size.width,20)];
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_W, SCREEN_H-TOPNAVIGATIONBAR_H-downViewH) collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.collectionView];
    
    self.collectionView.collectionViewLayout = layout;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[ClassForCollectionViewCell class] forCellWithReuseIdentifier:@"ClassForCollectionViewCell"];
    [self.collectionView registerClass:[SearchCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SearchCollectionReusableView"];
    
    _downView.backgroundColor = [UIColor whiteColor];
    
    [_downView setBianKuangAndColor:RGBColor(235, 235, 235)];
//    _downView.backgroundColor = [UIColor greenColor];
    [_addShopButton setTitle:@"去购物车结算" forState:UIControlStateNormal];
    [_addShopButton setTitleColor:RGBColorAlpha(208, 63, 81, 1) forState:UIControlStateNormal];
    _addShopButton.titleLabel.font = Cell_Medium_Font;
    _addShopButton.backgroundColor = RGBColorAlpha(44, 44, 44, 1);
    [_addShopButton.titleLabel sizeToFit];
    [_addShopButton sizeToFit];
    
    [_strollButton setTitle:@"继续逛逛" forState:UIControlStateNormal];
    [_strollButton setTitleColor:ShenZi_Color forState:UIControlStateNormal];
    _strollButton.titleLabel.font = Cell_Medium_Font;
    _strollButton.backgroundColor = RGBColorAlpha(235, 235, 235, 1);
    [_strollButton.titleLabel sizeToFit];
    [_strollButton sizeToFit];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark --- UICollectionViewDelegate

- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    return YES;
}

- (BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    return YES;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
}
- (UICollectionReusableView *) collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    ClassModel *model = [[ClassModel alloc]init];
    
    SearchCollectionReusableView *headerView = [self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SearchCollectionReusableView" forIndexPath:indexPath];
    //    headerView.backgroundColor = [UIColor greenColor];
    for (UIView * view in headerView.subviews) {
        [view removeFromSuperview];
    }
    CGFloat jianGeH = 45;
    CGFloat jianGeW = 25;
    CGFloat i_imageW= SCREEN_W-jianGeW*2;
    CGFloat i_imageH = SCREEN_W/(972/227.);
    CGFloat y_imageW = 98;
    CGFloat y_imageH = y_imageW;
    CGFloat top = 40; // 顶端盖高度
    CGFloat bottom = 80 ; // 底端盖高度
    CGFloat left = 20; // 左端盖宽度
    CGFloat right = 20; // 右端盖宽度
    
    UIImage * imageStr = nil;
    if (_isShow) {
        imageStr = [UIImage imageNamed:@"shop_anback"];
    }else{
        imageStr = [UIImage imageNamed:@"shop_lightback"];
    }
    UIImageView * image = [[UIImageView alloc]initWithFrame:CGRectMake(jianGeW, jianGeH, i_imageW, i_imageH)];
    imageStr = [imageStr stretchImageWithInsetsTop:top left:left bottom:bottom right:right];
    image.image = imageStr;
    
//    image.contentMode = UIViewContentModeScaleAspectFit;
    image.userInteractionEnabled = YES;
    CGFloat d_buttonW = 260;
    CGFloat d_buttonH = 60;
    
    UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake((i_imageW-d_buttonW)/2.0, i_imageH-d_buttonH, d_buttonW, d_buttonH)];
    [button addTarget:self action:@selector(buttonFunc:) forControlEvents:UIControlEventTouchUpInside];
    
    [image addSubview:button];
    
    UIImageView * y_image = [[UIImageView alloc]initWithFrame:CGRectMake((SCREEN_W-y_imageW)/2.0, 5, y_imageW, y_imageH)];
    y_image.image = [UIImage imageNamed:@"shop_yuan"];
    y_image.contentMode = UIViewContentModeScaleAspectFit;
    UILabel * titleLabel = [[UILabel alloc]init];
    titleLabel.text  =@"您的定制物品已成功加入购物车!";
    titleLabel.textColor = ShenZi_Color;
    titleLabel.font  = [UIFont systemFontOfSize:18];
    [titleLabel sizeToFit];
    titleLabel.frame = CGRectMake((SCREEN_W-titleLabel.frame.size.width)/2.0,(i_imageH-30)/2.0+25, titleLabel.frame.size.width, 30);
    UILabel * countLabel = [[UILabel alloc]init];
    NSString * number = @"12345";
    NSString * needStr = [NSString stringWithFormat:@"当前购物车已包含%@个订单",number];
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:needStr];
    
    // 设置字体和设置字体的范围
    [attrStr addAttribute:NSFontAttributeName
                    value:[UIFont systemFontOfSize:12.0f]
                    range:NSMakeRange(0, needStr.length)];
    // 添加文字颜色
    [attrStr addAttribute:NSForegroundColorAttributeName
                    value:QianZi_color
                    range:NSMakeRange(0, needStr.length)];
    // 添加文字颜色
    [attrStr addAttribute:NSForegroundColorAttributeName
                    value:RGBColorAlpha(255, 66, 89, 1)
                    range:NSMakeRange(8, number.length)];
    // 设置字体和设置字体的范围
    [attrStr addAttribute:NSFontAttributeName
                    value:[UIFont systemFontOfSize:15.0f]
                    range:NSMakeRange(8, number.length)];
    countLabel.attributedText = attrStr;
//    countLabel.textColor = QianZi_color;
//    countLabel.font  = [UIFont systemFontOfSize:14];
    [countLabel sizeToFit];
    countLabel.frame = CGRectMake((SCREEN_W-countLabel.frame.size.width)/2.0, titleLabel.frame.origin.y+titleLabel.frame.size.height, countLabel.frame.size.width, 30);
    CGFloat newsViewH = 0;
    if (_isShow) {
        newsViewH = 40*6;
        UIView * newsView = [[UIView alloc]initWithFrame:CGRectMake(jianGeW, image.frame.origin.y+image.frame.size.height+10, SCREEN_W-jianGeW*2, newsViewH)];
        newsView.backgroundColor = TAB_BAR_COLOR;
        [headerView addSubview:newsView];
        newsView.layer.shadowColor = RGBColorAlpha(205, 205, 205, 1).CGColor;//shadowColor阴影颜色
        newsView.layer.shadowOffset = CGSizeMake(0,-2);//shadowOffset阴影偏移,x向右偏移4，y向下偏移4，默认(0, -3),这个跟shadowRadius配合使用
        newsView.layer.shadowOpacity = 0.8;//阴影透明度，默认0
        newsView.layer.shadowRadius = 1;//阴影半径，默认3
        [self loadHeadForNewsView:newsView];
        
    }
    UIView * nameView = [[UIView alloc]initWithFrame:CGRectMake(0, image.frame.origin.y+image.frame.size.height+30+newsViewH, SCREEN_W, 60)];
    nameView.backgroundColor = RGBColorAlpha(234, 234, 234, 1);
    UILabel * _centerLabel = [[UILabel alloc]init];
    _centerLabel.text = @"为您甄选";
    _centerLabel.font = [UIFont systemFontOfSize:20];
    [_centerLabel sizeToFit];
    jianGeH = 5;
    CGFloat imageW = 17;
    CGFloat imageH = 25;
    CGFloat wid = imageW*2+10+_centerLabel.frame.size.width;
    
    UIImageView * _leftImage = [[UIImageView alloc]init];
    _leftImage.frame = CGRectMake((SCREEN_W-wid)/2.0, jianGeH, imageW, imageH);
    _leftImage.image = [UIImage imageNamed:@"@left"];
    _centerLabel.frame = CGRectMake(_leftImage.frame.origin.x+_leftImage.frame.size.width+5, jianGeH, _centerLabel.frame.size.width, 30);
    UIImageView * _rightImage = [[UIImageView alloc]init];
    _rightImage.frame = CGRectMake(_centerLabel.frame.origin.x+_centerLabel.frame.size.width+5, jianGeH, imageW, imageH);
    _rightImage.image = [UIImage imageNamed:@"@right"];
    UILabel * _contentLabel = [[UILabel alloc]init];
    _contentLabel.text = @"Selection for you";
    _contentLabel.textColor = QianZi_color;
    _contentLabel.font = Cell_Big_Font;
    [_contentLabel sizeToFit];
    _contentLabel.frame = CGRectMake((SCREEN_W-_contentLabel.frame.size.width)/2.0, _centerLabel.frame.origin.y+_centerLabel.frame.size.height, _contentLabel.frame.size.width, Nomal_Label_H);
    
    [nameView addSubview:_centerLabel];
    [nameView addSubview:_leftImage];
    [nameView addSubview:_rightImage];
    [nameView addSubview:_contentLabel];
    [headerView addSubview:nameView];
    [headerView addSubview:image];
    [headerView addSubview:y_image];
    [headerView addSubview:titleLabel];
    [headerView addSubview:countLabel];
    headerView.frame = CGRectMake(0, 0, SCREEN_W, nameView.frame.origin.y+nameView.frame.size.height);
    return headerView;
}
//加载客户订单信息View
-(void)loadHeadForNewsView:(UIView*)view
{
    
    CGFloat jianGeW = 25;
    CGFloat lableH = 40;
    CGFloat imagew = 194;
    CGFloat imageH = 414;
    CGFloat b_ViewW = view.frame.size.width;
    CGFloat viewW = (b_ViewW-jianGeW*4-194)/2.0;
    CGFloat viewH = lableH*6;
    
    UIView * leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, viewW, viewH)];
    [self loadNewsViewForNews:leftView andIsLeft:YES];
    [view addSubview:leftView];
    UIImageView * image = [[UIImageView alloc]initWithFrame:CGRectMake((b_ViewW-imagew)/2.0, 0, imagew, viewH)];
    image.image = [UIImage imageNamed:@"shop_nan"];
    image.contentMode = UIViewContentModeScaleAspectFit;
    [view addSubview:image];
    UIView * rightView = [[UIView alloc]initWithFrame:CGRectMake(b_ViewW-viewW-jianGeW*2, 0, viewW, viewH)];
    [self loadNewsViewForNews:rightView andIsLeft:NO];
    [view addSubview:rightView];
}
//是否是左边view
-(void)loadNewsViewForNews:(UIView*)view andIsLeft:(BOOL)isLeft
{
    NSArray * l_nameArr = [NSArray arrayWithObjects:@"产品名称：",@"订单号：",@"面料号：",@"下单日期：",@"包含物品：",@"备注：", nil];
    NSArray * l_valueArr = [NSArray arrayWithObjects:@"产品名称：value",@"订单号：value",@"面料号：value",@"下单日期：value",@"包含物品：value",@"备注：value", nil];
    NSArray * r_nameArr = [NSArray arrayWithObjects:@"客户姓名：",@"身高：",@"体重：",@"量体人员：",@"电话：",@"数量：", nil];
    NSArray * r_valueArr = [NSArray arrayWithObjects:@"客户姓名：value",@"身高：value",@"体重：value",@"量体人员：value",@"电话：value",@"数量：value", nil];
    CGFloat jianGeW = 45;
    CGFloat lableH = 40;
    for (int i=0; i<l_nameArr.count; i++) {
        
        UILabel * nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(jianGeW, 0, 80, lableH)];
        nameLabel.textColor = QianZi_color;
        nameLabel.font = Cell_Little_Font;
        [view addSubview:nameLabel];
        UILabel * valueLabel = [[UILabel alloc]initWithFrame:CGRectMake(nameLabel.frame.size.width+nameLabel.frame.origin.x, 0, 80, lableH)];
        valueLabel.textColor = ZhongZi_Color;
        valueLabel.font = Cell_Medium_Font;
        valueLabel.textAlignment = NSTextAlignmentRight;
        [view addSubview:valueLabel];
        
        if (isLeft) {
            nameLabel.text = [l_nameArr objectAtIndex:i];
            valueLabel.text = [l_valueArr objectAtIndex:i];
            nameLabel.frame = CGRectMake(jianGeW, lableH*i, 85, lableH);
        }else{
            
            nameLabel.text = [r_nameArr objectAtIndex:i];
            valueLabel.text = [r_valueArr objectAtIndex:i];
            nameLabel.frame = CGRectMake(0, lableH*i, 85, lableH);
        }
        [valueLabel sizeToFit];
        
        valueLabel.frame = CGRectMake(nameLabel.frame.size.width+nameLabel.frame.origin.x, lableH*i, view.frame.size.width-85-10, lableH);
        
    }
}
-(CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (_isShow) {
        CGSize size = {SCREEN_W, 140+SCREEN_W/(972/227.)+240};
        return size;
    }else{
        CGSize size = {SCREEN_W, 140+SCREEN_W/(972/227.)};
        return size;
    }
    
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(itemWidth, itemHeight);
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    // 需要配置的代码
    //    [collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
    ClassForCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ClassForCollectionViewCell" forIndexPath:indexPath];
    ClassModel * tmpModel = [[ClassModel alloc]init];
    tmpModel = [_dataArray objectAtIndex:indexPath.row];
    tmpModel.index = indexPath;
    tmpModel.name = @"测试数据";
    tmpModel.imageUrl = @"nv1";
//    cell.backgroundColor = RGBColorAlpha(245, 245, 245, 0.9);
    [cell setCellWithModel:tmpModel];
    return cell;
    
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
//    [_collectionView reloadData];
//    [_collectionView reloadItemsAtIndexPaths:[_collectionView indexPathsForVisibleItems]];
//    [_collectionView reloadData];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - 手势按钮方法
-(void)buttonFunc:(UIButton*)button
{
    _isShow = !_isShow;
    [_collectionView reloadData];
}
- (IBAction)strollButton:(UIButton *)sender {
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (IBAction)addShopButton:(UIButton *)sender {
}
@end
